//
//  PWDigitalUtils.m
//  Runner
//
//  Created by frank on 2024/5/20.
//

#import "PWDigitalUtils.h"

@implementation PWDigitalUtils

+ (NSString *)formatFloatNumberWithDecimalDigits:(float)number decimalDigits:(NSInteger)decimalDigits {
    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    [formatter setNumberStyle:NSNumberFormatterDecimalStyle];
    [formatter setMaximumFractionDigits:decimalDigits];
//    [formatter setRoundingMode:NSNumberFormatterRoundDown]; // 设置向下截取
    return [formatter stringFromNumber:@(number)];
}

+ (NSArray<NSNumber *> *) toBytesFromNumStr:(NSString *) numStr {
    NSInteger len = numStr.length % 2 == 0 ? numStr.length / 2 : numStr.length / 2 + 1;
    
    NSMutableArray<NSNumber *> *array = [NSMutableArray arrayWithCapacity:len];
    ///跳出循环条件
    NSInteger condition = 0;
    ///数组下标
    NSInteger index = 0;
    
    while (condition < numStr.length) {

        int length = (numStr.length - condition) > 1 ? 2 : 1;
        NSString *s = [numStr substringWithRange:NSMakeRange(condition, length)];
        int value = s.intValue;
        
        [array addObject:@(value)];

        condition += length;
        index ++;
    }
    
    
    return array;
}

+ (NSArray<NSNumber *> *) getBytesWith:(NSString *) src {
    return [self getBytesWith:src charsetName:@"GBK"];
}

+ (NSArray<NSNumber *> *) getBytesWith:(NSString *) src charsetName:(NSString *) charsetName {
    NSStringEncoding encoding = CFStringConvertEncodingToNSStringEncoding(CFStringConvertIANACharSetNameToEncoding((CFStringRef)charsetName));
    NSData* byteData = [src dataUsingEncoding:encoding];
    
    const void *bytes = [byteData bytes];
    NSUInteger length = [byteData length];
    Byte *byteArray = (Byte *)malloc(length);
    memcpy(byteArray, bytes, length);
    
    NSMutableArray<NSNumber *> *array = [NSMutableArray array];
    
    for (int i = 0; i < [byteData length]; i ++) {
        [array addObject:@(byteArray[i])];
    }
    
    return array;
}

+ (NSArray<NSNumber *> *) getBytesWith:(NSString *) src charsetName:(NSString *) charsetName destLen:(NSInteger) destLen {
    NSMutableArray<NSNumber *> *destArray = [NSMutableArray arrayWithArray:[self getBytesWith:src charsetName:charsetName]];
    
    NSInteger index = destArray.count;
    while (index < destLen) {
        [destArray addObject:@(32)];
        index ++;
    }
    
    return destArray;
}

+ (NSString *) toStringFromIntArray:(NSData *) data {
    
//    NSMutableData *data = [NSMutableData data];
//    for (NSNumber *number in array) {
//        unsigned char byte = [number unsignedCharValue];
//        [data appendBytes:&byte length:sizeof(unsigned char)];
//    }
    
    NSStringEncoding encoding = CFStringConvertEncodingToNSStringEncoding(CFStringConvertIANACharSetNameToEncoding((CFStringRef)@"GBK"));
    NSString *string = [[NSString alloc] initWithData:data encoding:encoding];
    
    return string;
}

+ (void) convertToHexArray:(NSArray<NSNumber *> *) decimalArray {
    NSMutableArray *hexArray = [NSMutableArray array];

    for (NSNumber *decimalNumber in decimalArray) {
        unsigned int decimalValue = [decimalNumber unsignedIntValue];
        NSString *hexString = [NSString stringWithFormat:@"%02X", decimalValue]; // %02X确保输出的十六进制是两位，如01、0A、1F等
        [hexArray addObject:hexString];
    }

    NSLog(@"Hex Array: %@", hexArray);
}

@end

